//
//  ForgotVCViewController.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ForgotVCViewController.h"

@interface ForgotVCViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *number;
@property (weak, nonatomic) IBOutlet UITextField *numberTwo;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *registButton;

@property (nonatomic, strong)UIButton *button;

@property (nonatomic, assign)int codeNum;

@end

@implementation ForgotVCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _codeNum = 60;
    
    // Do any additional setup after loading the view from its nib.
    _phone.layer.masksToBounds = YES;
    _phone.layer.cornerRadius = 20;
    _phone.layer.borderWidth = 1;
    _phone.layer.borderColor = hui1Color.CGColor;
    
    
    _phone.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _phone.leftViewMode = UITextFieldViewModeAlways;
    
    
    _number.layer.masksToBounds = YES;
    _number.layer.cornerRadius = 20;
    _number.layer.borderWidth = 1;
    _number.layer.borderColor = hui1Color.CGColor;
    
    
    _number.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _number.leftViewMode = UITextFieldViewModeAlways;
    
    _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    [_button setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    [_button setTitleColor:ZhuTiColor forState:(UIControlStateNormal)];
    //    button.layer.borderWidth = 1;
    //    button.layer.borderColor = ZhuTiColor.CGColor;
    BTN_MSG_FUNC(_button, getNumberButton:);
    _button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    _button.frame = CGRectMake(0, 0, 90, 40);
    
    _number.rightView = _button;
    _number.rightViewMode = UITextFieldViewModeAlways;
    
    _numberTwo.layer.masksToBounds = YES;
    _numberTwo.layer.cornerRadius = 20;
    _numberTwo.layer.borderWidth = 1;
    _numberTwo.layer.borderColor = hui1Color.CGColor;
    _numberTwo.secureTextEntry = YES;
    _numberTwo.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _numberTwo.leftViewMode = UITextFieldViewModeAlways;
    
    
    _password.layer.masksToBounds = YES;
    _password.layer.cornerRadius = 20;
    _password.layer.borderWidth = 1;
    _password.layer.borderColor = hui1Color.CGColor;
    _password.secureTextEntry = YES;
    _password.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 1)];
    _password.leftViewMode = UITextFieldViewModeAlways;
    
    
    _registButton.layer.masksToBounds = YES;
    _registButton.layer.cornerRadius = 20;
    [_registButton addTarget:self action:@selector(okBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
}

-(void)okBtnAction{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_phone.text,
                       @"sms_code":_number.text,
                       @"type":@"backPwd",
                       @"new_password1":_numberTwo.text,
                       @"new_password2":_password.text,
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=changePassword",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
            
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)getNumberButton:(UIButton *)sender{
    NSLog(@"获取验证码");
    
    if (_phone.text.length == 11) {
        
    }else{
        
        [MBProgressHUD showTextOnly:self.view message:@"请输入正确的手机号"];
        return;
        
    }
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"phone":_phone.text,
                       @"type":@"backPwd"
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Register&a=getSmsCode",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        if ([dic[@"status"]intValue] == 200) {
            
            [MBProgressHUD showTextOnly:self.view message:@"验证码发送成功" delay:2.0];
            
            [NSTimer scheduledTimerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(handleMaxShowTimer:)
                                           userInfo:nil
                                            repeats:YES];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    
    
    
}

#pragma mark  - 验证码倒计时
- (void)handleMaxShowTimer:(NSTimer *)timer{
    
    _codeNum--;
    [_button setTitle:[NSString stringWithFormat:@"重新获取(%ld)",(long)_codeNum]   forState:UIControlStateNormal];
    _button.userInteractionEnabled = NO;
    if (_codeNum == 0) {
        NSLog(@"验证码超时");
        //        _codeNum = @"fsdsa";
        [_button setTitle:@"重新获取" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        _codeNum = 60;
        _number.userInteractionEnabled = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
